package com.example.tao.mycar.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.login.LoginActivity;
import com.example.tao.mycar.utils.FileUtils;
import com.example.tao.mycar.utils.PicOperator;

import java.io.File;

public class HomeActivity extends MyActionBarActivity implements View.OnClickListener {

    private TextView weightNum, heightNum;
    private LinearLayout heightLayout, weightLayout, mileageLayout;
    private ImageView babyInfo;

    private static final String FILE_FOLDER_NAME = "Mycar";
    private String IMAGE_ICON_PATH_STR = "";
    private String HEAD_ICON_NAME = null;
    private Uri IMAGE_ICON_PATH_URI = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_home);
        setToolBar(getResources().getString(R.string.app_name), false);

        if (MyApplication.DEBUG = false) {
            HEAD_ICON_NAME = AVUser.getCurrentUser().getObjectId() + "_head_icon.png";
            IMAGE_ICON_PATH_STR = FileUtils.gainSDCardPath() + File.separator + FILE_FOLDER_NAME + File.separator + "icon" + File.separator + HEAD_ICON_NAME;
        }

        heightLayout = (LinearLayout)this.findViewById(R.id.height_layout);
        weightLayout = (LinearLayout)this.findViewById(R.id.weight_layout);
        mileageLayout = (LinearLayout)this.findViewById(R.id.mileage_layout);
        babyInfo = (ImageView)this.findViewById(R.id.baby_image);
        weightNum = (TextView)this.findViewById(R.id.weight_num);
        heightNum = (TextView)this.findViewById(R.id.height_num);

        if (FileUtils.fileExists(IMAGE_ICON_PATH_STR)) {
            IMAGE_ICON_PATH_URI = Uri.parse("file://" + IMAGE_ICON_PATH_STR);
            Bitmap bitmap = PicOperator.decodeUriAsBitmap(this, IMAGE_ICON_PATH_URI);
            babyInfo.setImageBitmap(PicOperator.toRoundBitmap(bitmap));
        } else {
            babyInfo.setImageResource(R.mipmap.homepage_headdata_bg_nor);
        }

        weightLayout.setOnClickListener(this);
        heightLayout.setOnClickListener(this);
        mileageLayout.setOnClickListener(this);
        babyInfo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.height_layout :
                Intent intentHeight = new Intent(this, UserInfoActivity.class);
                intentHeight.putExtra("position", 0);
                Log.d(MyApplication.TAG, "" + 0);
                startActivity(intentHeight);
                //Intent intentBabyInfo1 = new Intent(this, BabyInfoActivity.class);
                //startActivity(intentBabyInfo1);
                break;
            case R.id.weight_layout :
                Intent intentWeight = new Intent(this, UserInfoActivity.class);
                intentWeight.putExtra("position", 1);
                startActivity(intentWeight);
                break;
            case R.id.mileage_layout :
                Intent intentMileage = new Intent(this, UserInfoActivity.class);
                intentMileage.putExtra("position", 2);
                startActivity(intentMileage);
                break;
            case R.id.baby_image :
                Intent intentBabyInfo = new Intent(this, BabyInfoActivity.class);
                startActivity(intentBabyInfo);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = new MenuInflater(this);
        menuInflater.inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home :
                finish();
                break;
            case R.id.menu_device :

                break;
            case R.id.menu_logout :
                AVUser.logOut();

                startActivity(new Intent(this, LoginActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }
}
